package map.android.baidu.carpool.widget;

import java.io.Serializable;
import java.util.List;

import com.baidu.components.api.extra.APIProxy;
import com.baidu.platform.comapi.basestruct.Point;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import map.android.baidu.carpool.R;

public class TextinputHistroyAndSugListView extends ListView {
	public static final String LOGTAG = "TextinputHistroyAndSugListView";
	private View mHistoryFootView;
	private TextInputHistoryAndSugAdapter mAdapter;
	private OnClickListener mFooterOnClickListener;

	public TextinputHistroyAndSugListView(Context context) {
		this(context, null);
	}

	public TextinputHistroyAndSugListView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public TextinputHistroyAndSugListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public void setFooterClickListener(OnClickListener listener) {
		if (mHistoryFootView != null) {
			mFooterOnClickListener = listener;
			mHistoryFootView.setOnClickListener(listener);
		}
	}

	public void setHistoryContentList(List<TextInputSug> histroyList) {
		mAdapter.setHistoryContentList(histroyList);
		showFooterHistory();
	}

	public void setSugContentList(List<TextInputSug> sugList) {
		mAdapter.setSugContentList(sugList);
		if (sugList != null && sugList.size() > 0) {
			hideFooter();
		} else {
			showFooterNoSug();
		}
	}

	private void init(Context context) {
		initHistoryListViewFooter();
		mAdapter = new TextInputHistoryAndSugAdapter(context);
		setAdapter(mAdapter);
	}

	private void initHistoryListViewFooter() {
		if (mHistoryFootView == null) {
			mHistoryFootView = APIProxy.res().inflate(R.layout.carpool_history_sug_footer, this, false);
			addFooterView(mHistoryFootView);
		}
	}

	public void hideFooter() {
		if (mHistoryFootView != null) {
			mHistoryFootView.setVisibility(View.GONE);
			// AbsListView.LayoutParams params = new AbsListView.LayoutParams(
			// AbsListView.LayoutParams.MATCH_PARENT, 1);
			// mHistoryFootView.setLayoutParams(params);
		}
	}

	private void showFooterNoSug() {
		showFooter(R.string.car_no_sugs, false);
	}

	private void showFooterHistory() {
		int textResId = R.string.car_no_history;
		if (mAdapter.getCount() > 0) {
			textResId = R.string.car_clear_history;
		}
		showFooter(textResId, true);
	}

	private void showFooter(int textResId, boolean clickable) {
		if (mHistoryFootView != null) {
			mHistoryFootView.setVisibility(View.VISIBLE);
			// AbsListView.LayoutParams params = new AbsListView.LayoutParams(
			// AbsListView.LayoutParams.MATCH_PARENT,
			// AbsListView.LayoutParams.MATCH_PARENT);
			// mHistoryFootView.setLayoutParams(params);
			mHistoryFootView.setOnClickListener(clickable ? mFooterOnClickListener : null);

			TextView clearTextView = (TextView) mHistoryFootView.findViewById(R.id.car_text_clear_history);
			clearTextView.setText(textResId);
		}
	}

	private class TextInputHistoryAndSugAdapter extends BaseAdapter {
		private static final int DISPALY_HISTORY = 1;
		private static final int DISPALY_SUG = 2;
		private List<TextInputSug> mList = null;
		private int mType = DISPALY_HISTORY;

		public TextInputHistoryAndSugAdapter(final Context context) {
			super();
		}

		public void setHistoryContentList(List<TextInputSug> historyList) {
			setContentList(historyList, DISPALY_HISTORY);
		}

		public void setSugContentList(List<TextInputSug> sugList) {
			setContentList(sugList, DISPALY_SUG);
		}

		private void setContentList(List<TextInputSug> list, int type) {
			mType = type;
			mList = list;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			if (mList == null) {
				return 0;
			}
			return mList.size();
		}

		@Override
		public Object getItem(final int position) {
			if (mList == null) {
				return null;
			}

			return mList.get(position);
		}

		@Override
		public long getItemId(final int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, final ViewGroup parent) {
			ViewHolder viewHolder = null;
			if (convertView == null || convertView.getTag() == null) {
				convertView = APIProxy.res().inflate(R.layout.carpool_textinput_history_sug_listview_item, parent,
						false);
				viewHolder = new ViewHolder();
				viewHolder.iconImageview = (ImageView) convertView.findViewById(R.id.car_image);
				viewHolder.sugNameTextView = (TextView) convertView.findViewById(R.id.car_text);
				viewHolder.sugAdressTextView = (TextView) convertView.findViewById(R.id.car_text_address);
				convertView.setTag(viewHolder);
			} else {
				Object tag = convertView.getTag();
				if (tag instanceof ViewHolder) {
					viewHolder = (ViewHolder) tag;
				} else {
					return convertView;
				}
			}
			if (mList == null) {
				return convertView;
			}
			
			TextInputSug sug = mList.get(position);
			handleTextInputSug(viewHolder, sug ,position == 0);

			return convertView;
		}

		private void handleTextInputSug(ViewHolder viewHolder, TextInputSug sug ,boolean isFirstSug) {
			if (sug == null) {
				return;
			}
			if (isFirstSug ) {
				viewHolder.sugNameTextView.setText("当前位置");
				viewHolder.sugAdressTextView.setText(sug.mAddress);
				viewHolder.iconImageview.setImageResource(R.drawable.carpool_myposition);
				viewHolder.sugNameTextView.setTextColor(getResources().getColor(R.color.car_text_blue));
			}else {
				viewHolder.sugNameTextView.setText(sug.getName());
				viewHolder.sugAdressTextView.setText(sug.mAddress);
				if (mType == DISPALY_HISTORY) {
					viewHolder.iconImageview.setImageResource(R.drawable.carpool_history);
				} else if (mType == DISPALY_SUG) {
					viewHolder.iconImageview.setImageResource(R.drawable.carpool_search);
				}
			}
		}

	}

	private class ViewHolder {
		public ImageView iconImageview;
		public TextView sugNameTextView;
		public TextView sugAdressTextView;
	}

	public static class TextInputSug implements Serializable {

		/**
		 * 
		 */
		private static final long serialVersionUID = 103721130138549768L;

		public String getName() {
			return mName;
		}

		public void setName(String name) {
			this.mName = name;
		}

		public String getAddress() {
			return mAddress;
		}

		public void setAddress(String address) {
			this.mAddress = address;
		}

		public void setPoint(Point point) {
			this.mPoint = point;
		}

		public Point getPoint() {
			return mPoint;
		}

		private String mName = "";
		private String mAddress = "";
		private Point mPoint;
	}
}
